package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    public String list(Model model, EmployeeQuery employeeQuery) {
        PageInfo employeeList = employeeService.list(employeeQuery);
        List<Department> depts = departmentService.listAll();
        model.addAttribute("depts", depts);
        model.addAttribute("result", employeeList);
        model.addAttribute("qo", employeeQuery);
        return "employee/list"; // /WEB-INF/views/employee/list.jsp
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"employee:input", "员工编辑"}, logical = Logical.OR)
    public String input(Long id, Model model) {
        //部门下拉框
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);
        if (id != null) { // 点击了修改按钮过来  编辑
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input"; // /WEB-INF/views/employee/input.jsp
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工保存/更新"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        System.out.println(employee);
        if (employee.getId() == null) { // 代表新增
            employeeService.save(employee, ids);
        } else { // 代表修改
            employeeService.update(employee, ids);
        }
        return new JsonResult();// http://localhost/employee/list.do
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        employeeService.delete(id);
        employeeService.deleteRelation(id);
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String name) {
        Employee employee = employeeService.selectByName(name);
        HashMap<String, Boolean> map = new HashMap();
        //插件需要加上valid
        map.put("valid", employee == null);
        return map;
    }

    @RequestMapping("/deleteBatch")
    @ResponseBody
    @RequiresPermissions(value = {"employee:deleteBatch", "员工批量删除"}, logical = Logical.OR)
    public JsonResult deleteBatch(Long[] ids) {
        employeeService.deleteBatch(ids);
        return new JsonResult();
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        /**
         * 使用poi的时候的代码
         */
        //在业务层创建excel文件
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
        /**
         * 使用阿里插件
         /**/
        /*List<Employee> employees = employeeService.listAll();
        EasyExcel.write("员工列表", Employee.class).sheet("员工名单")
                .doWrite(employees);*/
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.inportXls(file);
        return new JsonResult();
    }

    @RequestMapping("/test1")
    @ResponseBody
    public String test(MultipartFile file) throws IOException {
        return "hello";
    }
}
